<template>
  <transition name="expand" v-bind="transitions">
    <slot v-if="expand" />
  </transition>
</template>

<script lang="ts" setup>
import { ref, reactive } from 'vue';

const props = withDefaults(
  defineProps<{
    expand: boolean;
    direction: 'width' | 'height'; // 新增方向属性
  }>(),
  {
    expand: false,
    direction: 'height', // 默认方向为高度
  }
);

// 过渡时间
const transitions: Record<string, any> = {
  onBeforeEnter(el: HTMLElement) {
    el.style[props.direction] = '0';
    el.style.opacity = '0';
  },
  onEnter(el: HTMLElement) {
    el.style[props.direction] = `${el.scrollWidth}px`; // 根据方向动态设置宽度或高度
    if (props.direction === 'height') {
      el.style[props.direction] = `${el.scrollHeight}px`;
    } else {
      el.style[props.direction] = `${el.scrollWidth}px`;
    }
    el.style.opacity = '1';
  },
  onAfterEnter(el: HTMLElement) {
    el.style[props.direction] = '';
  },
  onBeforeLeave(el: HTMLElement) {
    el.style[props.direction] = `${el.scrollHeight}px`;
    if (props.direction === 'height') {
      el.style[props.direction] = `${el.scrollHeight}px`;
    } else {
      el.style[props.direction] = `${el.scrollWidth}px`;
    }
    el.style.opacity = '1';
  },
  onLeave(el: HTMLElement) {
    el.style[props.direction] = '0';
    el.style.opacity = '0';
  },
  onAfterLeave(el: HTMLElement) {
    el.style[props.direction] = '';
  },
};
</script>

<style lang="less" scoped>
// menu收缩动画
.expand-enter-active,
.expand-leave-active {
  transition:
    width 0.2s cubic-bezier(0.34, 0.69, 0.1, 1),
    height 0.2s cubic-bezier(0.34, 0.69, 0.1, 1),
    opacity 0.2s cubic-bezier(0.3, 1.3, 0.3, 1);
}
</style>